/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Demo.java
 *
 * Created on Jan 10, 2012, 4:59:27 PM
 */
package GUI;

import Model.ItemTableModel;
import Model.ItemUpdateListener;
import Model.UserModel;
import Entity.Category;
import Entity.ConnectionSQL;
import Entity.Item;
import Entity.User;
import java.awt.CardLayout;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;

public class AdminFrame extends javax.swing.JFrame {
    ConnectionSQL conn;
    ResourceBundle message;
    List<Category> listCategory;
    ItemTableModel itemModel;
    UserModel userModel;
    User user;
    TableRowSorter<ItemTableModel> sorterItem;
    TableRowSorter<UserModel> sorterUser;
    CardLayout cardLayout;
    
    public AdminFrame(User user) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DialogDetailBill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DialogDetailBill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DialogDetailBill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DialogDetailBill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        initComponents();
        this.setLocationRelativeTo(null);
        this.user = user;
        cardLayout = (CardLayout)pnlCenter.getLayout();
        cardLayout.addLayoutComponent(pnlStart, "pnlHome");
        cardLayout.addLayoutComponent(pnlUser, "pnlUser");
        cardLayout.addLayoutComponent(pnlItem, "pnlItem");
        
        
        runThreadInit();
        runThreadConnection();
    }
    
    private void runThreadInit(){
        new Thread("Thread init"){
            @Override
            public void run(){
                message = ResourceBundle.getBundle("messages", Locale.ENGLISH);
                conn = new ConnectionSQL(message);
                tblItem.setFillsViewportHeight(true);
                tblUser.setFillsViewportHeight(true);
                lblInfoUser.setText(user.getUserName());
               
                initDataUser();
                initDataItem();
            }
        }.start();
    }
    private void runThreadConnection(){
        new Thread("check connection"){
            @Override
            public void run(){
                if(conn == null)
                    lblConnect.setText("Connecting");
                else
                    lblConnect.setText("Lost connection");
            }
        }.start();
    }
    
    /**
     * init data of user
     */
    private void initDataUser(){

        userModel = new UserModel(conn.getUser(),message);
        tblUser.setModel(userModel);
        getUser();

        sorterUser = new TableRowSorter<UserModel>(userModel);
        tblUser.setRowSorter(sorterUser);

        TableColumn column;
        for(int i = 0;i< userModel.getColumnCount();i++){
            column = tblUser.getColumnModel().getColumn(i);
            if(i==0)
                column.setPreferredWidth(15);
            else if(i==1)
                column.setPreferredWidth(60);
            else if(i==2)
                column.setPreferredWidth(70);
            else if(i==3)
                column.setPreferredWidth(120);
            else if(i==4)
                column.setPreferredWidth(60);
            else if(i==5)
                column.setPreferredWidth(60);
            else if(i==6)
                column.setPreferredWidth(60);
            else if(i==7)
                column.setPreferredWidth(60);

        }
    }
    private void initDataItem(){
        getItem();
        getCategory();

        itemModel.setUpdateListener(new ItemUpdateListener() {

            @Override
            public void update(Item i) {
                boolean result = conn.updatePriceItem(i);
                if(!result)
                    System.out.println("Failed!");
            }
        });

        sorterItem= new TableRowSorter<ItemTableModel>(itemModel);
        tblItem.setRowSorter(sorterItem);

        TableColumn priceColumn = tblItem.getColumnModel().getColumn(2);
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("Click to change price");
        priceColumn.setCellRenderer(renderer);

        tblItem.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    /*
     * get user list for user table
     */
    private void getUser(){
        if(userModel.setList(conn.getUser())){
            lblNotifyUser.setText(""+userModel.getRowCount());
        }
    }
    private void getItem(){
        itemModel = new ItemTableModel(conn.getItem(), message);
        tblItem.setModel(itemModel);
        lblNotifyItem.setText(""+itemModel.getRowCount());
    }
    private void getCategory(){
        listCategory = conn.getCategory();
        lstCategory.setListData(new Vector(listCategory));
    }
    private void filterItem(){
        String text = lstCategory.getSelectedValue().toString();
        if (text.length() == 0) {
          sorterItem.setRowFilter(null);
        } else {
          sorterItem.setRowFilter(RowFilter.regexFilter(text));
        }
    }
    private void filterUser(){
        String text = lstPriority.getSelectedValue().toString();
        if(text.compareTo("All") == 0){
            sorterUser.setRowFilter(null);
        }else{
            sorterUser.setRowFilter(RowFilter.regexFilter(text));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlNorth = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        pnlCenter = new javax.swing.JPanel();
        pnlStart = new javax.swing.JPanel();
        pnlUser = new javax.swing.JPanel();
        pnlUserCenter = new javax.swing.JPanel();
        pnlUserList = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jPanel11 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstPriority = new javax.swing.JList();
        pnlUserTable = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        btnAddUser = new javax.swing.JButton();
        jPanel9 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblUser = new javax.swing.JTable();
        jPanel12 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lblNotifyUser = new javax.swing.JLabel();
        pnlItem = new javax.swing.JPanel();
        pnlItemWest = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        btnAddCategory = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        txtCategory = new javax.swing.JTextField();
        lblNotifyCategory = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstCategory = new javax.swing.JList();
        pnlItemCenter = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        btnAddItem = new javax.swing.JButton();
        btnShowAll = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jPanel13 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        lblNotifyItem = new javax.swing.JLabel();
        jPanel14 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tblItem = new javax.swing.JTable();
        pnlSouth = new javax.swing.JPanel();
        lblInfoUser = new javax.swing.JLabel();
        lblConnect = new javax.swing.JLabel();
        pnlFunction = new javax.swing.JPanel();
        btnHome = new javax.swing.JButton();
        btnUser = new javax.swing.JButton();
        btnItem = new javax.swing.JButton();
        btnConfigure = new javax.swing.JButton();
        btnSignOut = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sale And Inventory Application");

        pnlNorth.setPreferredSize(new java.awt.Dimension(724, 120));
        pnlNorth.setLayout(new java.awt.BorderLayout());

        jPanel3.setBackground(new java.awt.Color(0, 0, 0));
        jPanel3.setPreferredSize(new java.awt.Dimension(140, 120));

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/invent icon.png"))); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addContainerGap(12, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pnlNorth.add(jPanel3, java.awt.BorderLayout.LINE_START);

        jPanel5.setBackground(new java.awt.Color(0, 0, 0));
        jPanel5.setMinimumSize(new java.awt.Dimension(80, 100));
        jPanel5.setPreferredSize(new java.awt.Dimension(600, 129));

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/header.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap(493, Short.MAX_VALUE)
                .addComponent(jLabel3))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlNorth.add(jPanel5, java.awt.BorderLayout.CENTER);

        getContentPane().add(pnlNorth, java.awt.BorderLayout.PAGE_START);

        pnlCenter.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlCenter.setPreferredSize(new java.awt.Dimension(715, 370));
        pnlCenter.setLayout(new java.awt.CardLayout());

        javax.swing.GroupLayout pnlStartLayout = new javax.swing.GroupLayout(pnlStart);
        pnlStart.setLayout(pnlStartLayout);
        pnlStartLayout.setHorizontalGroup(
            pnlStartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 885, Short.MAX_VALUE)
        );
        pnlStartLayout.setVerticalGroup(
            pnlStartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 426, Short.MAX_VALUE)
        );

        pnlCenter.add(pnlStart, "card3");

        pnlUser.setLayout(new java.awt.BorderLayout(5, 5));

        pnlUserCenter.setLayout(new java.awt.BorderLayout());

        pnlUserList.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 144, Short.MAX_VALUE)
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        pnlUserList.add(jPanel10, java.awt.BorderLayout.PAGE_END);

        jPanel11.setLayout(new javax.swing.BoxLayout(jPanel11, javax.swing.BoxLayout.LINE_AXIS));

        lstPriority.setBackground(new java.awt.Color(240, 240, 240));
        lstPriority.setBorder(javax.swing.BorderFactory.createTitledBorder("List"));
        lstPriority.setFont(new java.awt.Font("Tahoma", 0, 12));
        lstPriority.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "All", "Sale", "Sale Manager", "Inventory Manager", "Admin" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstPriority.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstPriorityValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(lstPriority);

        jPanel11.add(jScrollPane2);

        pnlUserList.add(jPanel11, java.awt.BorderLayout.PAGE_START);

        pnlUserCenter.add(pnlUserList, java.awt.BorderLayout.LINE_START);

        pnlUserTable.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        pnlUserTable.setLayout(new java.awt.BorderLayout());

        jPanel7.setPreferredSize(new java.awt.Dimension(579, 48));

        btnAddUser.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Add.png"))); // NOI18N
        btnAddUser.setToolTipText("Add new user");
        btnAddUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddUserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(btnAddUser, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(683, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(btnAddUser, javax.swing.GroupLayout.PREFERRED_SIZE, 46, Short.MAX_VALUE)
                .addContainerGap())
        );

        pnlUserTable.add(jPanel7, java.awt.BorderLayout.PAGE_START);

        jPanel9.setLayout(new javax.swing.BoxLayout(jPanel9, javax.swing.BoxLayout.LINE_AXIS));

        tblUser.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblUser);

        jPanel9.add(jScrollPane1);

        pnlUserTable.add(jPanel9, java.awt.BorderLayout.CENTER);

        jPanel12.setPreferredSize(new java.awt.Dimension(586, 30));

        jLabel1.setText("Total: ");

        lblNotifyUser.setText("jLabel2");

        javax.swing.GroupLayout jPanel12Layout = new javax.swing.GroupLayout(jPanel12);
        jPanel12.setLayout(jPanel12Layout);
        jPanel12Layout.setHorizontalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel12Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblNotifyUser)
                .addContainerGap(658, Short.MAX_VALUE))
        );
        jPanel12Layout.setVerticalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel12Layout.createSequentialGroup()
                .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNotifyUser))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlUserTable.add(jPanel12, java.awt.BorderLayout.PAGE_END);

        pnlUserCenter.add(pnlUserTable, java.awt.BorderLayout.CENTER);

        pnlUser.add(pnlUserCenter, java.awt.BorderLayout.CENTER);

        pnlCenter.add(pnlUser, "card4");

        pnlItem.setLayout(new java.awt.BorderLayout());

        pnlItemWest.setPreferredSize(new java.awt.Dimension(160, 328));
        pnlItemWest.setLayout(new java.awt.BorderLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Control"));

        btnAddCategory.setText("Add");
        btnAddCategory.setToolTipText("Add new category");
        btnAddCategory.setPreferredSize(new java.awt.Dimension(60, 23));
        btnAddCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddCategoryActionPerformed(evt);
            }
        });

        btnClear.setText("Clear");
        btnClear.setToolTipText("Clear text fieldl");
        btnClear.setPreferredSize(new java.awt.Dimension(60, 23));
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        lblNotifyCategory.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblNotifyCategory.setForeground(new java.awt.Color(204, 0, 0));
        lblNotifyCategory.setText(" ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnAddCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(17, 17, 17)
                        .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtCategory, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                    .addComponent(lblNotifyCategory, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblNotifyCategory)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddCategory, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pnlItemWest.add(jPanel1, java.awt.BorderLayout.PAGE_END);

        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));

        lstCategory.setBackground(new java.awt.Color(240, 240, 240));
        lstCategory.setBorder(javax.swing.BorderFactory.createTitledBorder("Category"));
        lstCategory.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstCategoryValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(lstCategory);

        jPanel2.add(jScrollPane3);

        pnlItemWest.add(jPanel2, java.awt.BorderLayout.CENTER);

        pnlItem.add(pnlItemWest, java.awt.BorderLayout.LINE_START);

        pnlItemCenter.setBorder(javax.swing.BorderFactory.createTitledBorder("Item"));
        pnlItemCenter.setLayout(new java.awt.BorderLayout());

        jPanel4.setPreferredSize(new java.awt.Dimension(548, 50));

        btnAddItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Add.png"))); // NOI18N
        btnAddItem.setToolTipText("Add new item");
        btnAddItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddItemActionPerformed(evt);
            }
        });

        btnShowAll.setText("All");
        btnShowAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(btnAddItem, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnShowAll, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(617, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnShowAll, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
                    .addComponent(btnAddItem, javax.swing.GroupLayout.PREFERRED_SIZE, 46, Short.MAX_VALUE))
                .addContainerGap())
        );

        pnlItemCenter.add(jPanel4, java.awt.BorderLayout.PAGE_START);

        jPanel6.setLayout(new java.awt.BorderLayout());

        jPanel13.setPreferredSize(new java.awt.Dimension(548, 30));

        jLabel2.setText("Toal: ");

        lblNotifyItem.setText("jLabel3");

        javax.swing.GroupLayout jPanel13Layout = new javax.swing.GroupLayout(jPanel13);
        jPanel13.setLayout(jPanel13Layout);
        jPanel13Layout.setHorizontalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel13Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblNotifyItem)
                .addContainerGap(642, Short.MAX_VALUE))
        );
        jPanel13Layout.setVerticalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                .addComponent(lblNotifyItem))
        );

        lblNotifyItem.getAccessibleContext().setAccessibleName("");

        jPanel6.add(jPanel13, java.awt.BorderLayout.PAGE_END);

        jPanel14.setLayout(new javax.swing.BoxLayout(jPanel14, javax.swing.BoxLayout.LINE_AXIS));

        tblItem.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane4.setViewportView(tblItem);

        jPanel14.add(jScrollPane4);

        jPanel6.add(jPanel14, java.awt.BorderLayout.CENTER);

        pnlItemCenter.add(jPanel6, java.awt.BorderLayout.CENTER);

        pnlItem.add(pnlItemCenter, java.awt.BorderLayout.CENTER);

        pnlCenter.add(pnlItem, "card5");

        getContentPane().add(pnlCenter, java.awt.BorderLayout.CENTER);

        pnlSouth.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlSouth.setPreferredSize(new java.awt.Dimension(715, 30));

        lblInfoUser.setText("info");

        lblConnect.setText("jLabel5");

        javax.swing.GroupLayout pnlSouthLayout = new javax.swing.GroupLayout(pnlSouth);
        pnlSouth.setLayout(pnlSouthLayout);
        pnlSouthLayout.setHorizontalGroup(
            pnlSouthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSouthLayout.createSequentialGroup()
                .addComponent(lblInfoUser, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(344, 344, 344)
                .addComponent(lblConnect)
                .addContainerGap(591, Short.MAX_VALUE))
        );
        pnlSouthLayout.setVerticalGroup(
            pnlSouthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlSouthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(lblInfoUser, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                .addComponent(lblConnect))
        );

        getContentPane().add(pnlSouth, java.awt.BorderLayout.PAGE_END);

        pnlFunction.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Function", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));
        pnlFunction.setPreferredSize(new java.awt.Dimension(170, 387));

        btnHome.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/hom.png"))); // NOI18N
        btnHome.setText("HOME");
        btnHome.setToolTipText("Home");
        btnHome.setPreferredSize(new java.awt.Dimension(155, 60));
        btnHome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHomeActionPerformed(evt);
            }
        });
        pnlFunction.add(btnHome);

        btnUser.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/index_img71.gif"))); // NOI18N
        btnUser.setText("<html>\nUSER</br>\nMANAGEMENT\n</html>");
        btnUser.setPreferredSize(new java.awt.Dimension(157, 60));
        btnUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUserActionPerformed(evt);
            }
        });
        pnlFunction.add(btnUser);

        btnItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Books.png"))); // NOI18N
        btnItem.setText("<html>\nITEM<br/>\nMANAGEMENT\n<html>");
        btnItem.setToolTipText("Item Management");
        btnItem.setPreferredSize(new java.awt.Dimension(155, 60));
        btnItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnItemActionPerformed(evt);
            }
        });
        pnlFunction.add(btnItem);

        btnConfigure.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/service_manager.png"))); // NOI18N
        btnConfigure.setText("<html> CONFIGURE<br/> SYSTEM <html>");
        btnConfigure.setToolTipText("Reset connection");
        btnConfigure.setPreferredSize(new java.awt.Dimension(155, 60));
        btnConfigure.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfigureActionPerformed(evt);
            }
        });
        pnlFunction.add(btnConfigure);

        btnSignOut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Padlock.png"))); // NOI18N
        btnSignOut.setText("SIGN OUT");
        btnSignOut.setToolTipText("Sign Out");
        btnSignOut.setPreferredSize(new java.awt.Dimension(155, 60));
        btnSignOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSignOutActionPerformed(evt);
            }
        });
        pnlFunction.add(btnSignOut);

        getContentPane().add(pnlFunction, java.awt.BorderLayout.LINE_START);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnItemActionPerformed
        cardLayout.show(pnlCenter, "pnlItem");
    }//GEN-LAST:event_btnItemActionPerformed

    private void btnAddUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddUserActionPerformed
        new DialogAddUser(this, rootPaneCheckingEnabled, userModel).setVisible(true);
        User u = userModel.findUser(-1);
        if(u!= null){
            conn.saveUser(u);
            userModel.fireTableDataChanged();
        }
    }//GEN-LAST:event_btnAddUserActionPerformed

    private void btnAddItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddItemActionPerformed
        new DialogAddItem(this, rootPaneCheckingEnabled,listCategory,itemModel).setVisible(true);
        Item item = itemModel.findItem(-1);
        if(item!=null){
            conn.saveItem(item);
            itemModel.fireTableDataChanged();
        }
    }//GEN-LAST:event_btnAddItemActionPerformed

    private void btnAddCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddCategoryActionPerformed
        String strCate = txtCategory.getText().trim();
        if(!itemModel.checkCategoryName(strCate)){
            lblNotifyCategory.setText("***Wrong***");
        }else{
            Category cate = new Category(-1, strCate);
            if(conn.saveCategory(cate)){
                lblNotifyCategory.setText("1 category is added");
                listCategory = conn.getCategory();
                lstCategory.setListData(new Vector(listCategory));
            }
            else
                lblNotifyCategory.setText("Failed!Please revise the connection");
            txtCategory.setText("");
        }

    }//GEN-LAST:event_btnAddCategoryActionPerformed

    private void btnHomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHomeActionPerformed
        cardLayout.show(pnlCenter, "pnlHome");
    }//GEN-LAST:event_btnHomeActionPerformed


    private void btnSignOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSignOutActionPerformed
        this.dispose();
        try {
                    UIManager.setLookAndFeel("com.jtattoo.plaf.graphite.GraphiteLookAndFeel");
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(LoginFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(LoginFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(LoginFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(LoginFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
        java.awt.EventQueue.invokeLater(new Runnable() {

                public void run() {
                    new LoginFrame().setVisible(true);

                }
            });
    }//GEN-LAST:event_btnSignOutActionPerformed

    private void lstCategoryValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstCategoryValueChanged
        filterItem();
    }//GEN-LAST:event_lstCategoryValueChanged

    private void btnConfigureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfigureActionPerformed
        new DialogConfig(this, rootPaneCheckingEnabled, message).setVisible(true);
    }//GEN-LAST:event_btnConfigureActionPerformed

    private void lstPriorityValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstPriorityValueChanged
        filterUser();
    }//GEN-LAST:event_lstPriorityValueChanged

    private void btnShowAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowAllActionPerformed
        sorterItem.setRowFilter(null);
    }//GEN-LAST:event_btnShowAllActionPerformed

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        txtCategory.setText("");
    }//GEN-LAST:event_btnClearActionPerformed

    private void btnUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUserActionPerformed
        cardLayout.show(pnlCenter, "pnlUser");
    }//GEN-LAST:event_btnUserActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddCategory;
    private javax.swing.JButton btnAddItem;
    private javax.swing.JButton btnAddUser;
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnConfigure;
    private javax.swing.JButton btnHome;
    private javax.swing.JButton btnItem;
    private javax.swing.JButton btnShowAll;
    private javax.swing.JButton btnSignOut;
    private javax.swing.JButton btnUser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JLabel lblConnect;
    private javax.swing.JLabel lblInfoUser;
    private javax.swing.JLabel lblNotifyCategory;
    private javax.swing.JLabel lblNotifyItem;
    private javax.swing.JLabel lblNotifyUser;
    private javax.swing.JList lstCategory;
    private javax.swing.JList lstPriority;
    private javax.swing.JPanel pnlCenter;
    private javax.swing.JPanel pnlFunction;
    private javax.swing.JPanel pnlItem;
    private javax.swing.JPanel pnlItemCenter;
    private javax.swing.JPanel pnlItemWest;
    private javax.swing.JPanel pnlNorth;
    private javax.swing.JPanel pnlSouth;
    private javax.swing.JPanel pnlStart;
    private javax.swing.JPanel pnlUser;
    private javax.swing.JPanel pnlUserCenter;
    private javax.swing.JPanel pnlUserList;
    private javax.swing.JPanel pnlUserTable;
    private javax.swing.JTable tblItem;
    private javax.swing.JTable tblUser;
    private javax.swing.JTextField txtCategory;
    // End of variables declaration//GEN-END:variables
}

